<template>
	<u-popup :show="userUp" mode="bottom" round="60"  :closeOnClickOverlay="true" @close="close">
		<view class="pop_content">
			<view class="pop_title">
				获取你的头像、昵称
			</view>
			<view class="des">
				<view class="xin1">
					<button type="default" class="btncss" open-type="chooseAvatar" @chooseavatar="chooseavatar">
						<view class="toubox">
							<view class="tl">
								头像
							</view>
							<view class="avatarbox" v-if="!avatar">
								
							</view>
							<image v-else class="avatar_img" :src="avatar" ></image>
						</view>
						<u-icon name="arrow-right"></u-icon>
					</button>
				</view>
				<view class="nick">
					<view class="nl">
						昵称：
					</view>
					<input v-model="nickname" @input="getNewNickname" @change="getNewNickname" type="nickname" placeholder="请输入昵称" >
				</view>
			</view>
			<view class="buttons">
				
				<button  class="agree btns" id="agree-btn" @click="agree"
					>允许</button>
					<button  class="reject btns" @click="close">拒绝</button>
			</view>
		</view>
	</u-popup>
</template>

<script>
	import tools from "@/common/AES.js";
	import {updateInfo} from '@/api/my.js'
	export default{
		name:'userPopup',
		props:{
			userUp:{
				type:Boolean,
				default:true
			},
			avatar:{
				type:String,
				default:''
			},
			nickname:{
				type:String,
				default:''
			}
		},
		data(){
			return{
				
			}
		},
		methods:{
			agree(){
				if(!this.avatar){
					uni.showToast({
						icon:'none',
						title:'请上传头像'
					})
					return false;
				}
				if(!this.nickname){
					uni.showToast({
						icon:'none',
						title:'请填写昵称'
					})
					return false;
				}
				const data = {
					head_pic:this.avatar,
					nick_name:this.nickname
				}
				updateInfo(data).then(res=>{
					uni.setStorageSync('nickname',this.nickname)
					this.$emit("update:userUp",false)
					this.$emit("updateInfo",1)
				})
			},
			getNewNickname(e){
				this.$emit("update:nickname",e.detail.value)
			},
			chooseavatar(res){
				console.log(res,'res')
				const encrypt = tools.setAES(JSON.stringify({
						mall_id:30,
						anget_id:'',
						giftId:0
					}))
					const jwt = uni.getStorageSync('jwt');
					let that = this;
				uni.uploadFile({
					filePath: res.detail.avatarUrl,
					name: 'file',
					header:{
						Authorization:"bearer "+jwt
					},
					url:"https://saasmall.duobakeji.com/api/xcx/upload/avatar",			
					formData:{
						encrypt
					},
					success(res){
						console.log(res)
						const result = JSON.parse(res.data)
						that.$emit('update:avatar',result.data)
						uni.setStorageSync('avatar',result.data)
					}
				})
				
			},
			close(){
				console.log(1111)
				this.$emit('update:userUp',false)
			}
		}
	}
</script>

<style lang="scss">
	.pop_content {
		padding: 20px;
		background-color: #fff;
		text-align: justify;
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
		border-top-left-radius: 20px;
		border-top-right-radius: 20px;
	}
	.pop_title{
		font-size: 18px;
		font-weight: 700;
		color: #333;
		margin-bottom: 12px;
	}
	.link{
		color: #676b8e;
		padding-top: 12px;
	}
	.des{
		font-size: 14px;
		color: #999;
		margin-bottom: 12px;
	}
	.buttons .btns{
		font-size: 14px;
		
		color: #576a92;
	}
	.btns::after{
		border: none !important;
	}
	.agree{
		background-color: #e8f2fe;
		color: #2358b4;
	}
	.btncss{
		display: flex;
		border: none;
		background-color: transparent !important;
		justify-content: space-between;
		align-items: center;
		padding: 0;
		font-size: 16px;
		color: #333;
	}
	button.btncss::after {
		border: none;
		background-color: transparent;
	}
	.avatarbox{
		width: 40px;
		height: 40px;
		margin-left: 10px;
	}
	.avatar_img{
		width: 40px;
		height: 40px;
		border-radius: 50%;
		margin-left: 10px;
	}
	.xin1{
		border-bottom: 1px solid #ddd;
		padding: 12px 0;
	}
	.toubox{
		display: flex;
		align-items: center;
	}
	.nick{
		display: flex;
		align-items: center;
		font-size: 16px;
		color: #333;
		border-bottom: 1px solid #ddd;
		padding: 20px 0;
	}

</style>